home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
dev
/
c
/
cpost_1_4.lha
/
cpost.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-09
|
32KB
|
915 lines
/*------------------------------------------------------------------
* cPost.c : c language formatter
*------------------------------------------------------------------
* 10-10-91 originally by Patrick J. Mueller
* 12-03-92 converted from cBook to cPost
*------------------------------------------------------------------*/
#define PROGRAM_VERS "1.4"
#define PROGRAM_NAME "cPost"
#define PROGRAM_YEAR "1994"
#define PROGRAM_AUTH "Patrick J. Mueller"
#define PROGRAM_ADDR "(pmuellr@vnet.ibm.com)"
#define PROGRAM_ENVV "CPOST"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include "parsearg.h"
#include "ctok.h"
#define DEFINE_GLOBALS
#include "cpost.h"
#include "tokfile.h"
#include "cposthdr.h"
/*---------------------------------------------------------------
* global variables
*---------------------------------------------------------------*/
int AllDone = 0;
static char *ReservedTokens[] =
{
/*---------------------------------------------------------------
* data types
*---------------------------------------------------------------*/
"auto", "char", "const", "double", "enum", "extern", "float", "int",
"long", "register", "short", "signed", "static", "struct", "union",
"unsigned", "void", "volatile",
/*---------------------------------------------------------------
* other keywords
*---------------------------------------------------------------*/
"break", "case", "continue", "default", "do", "else", "for", "goto",
"if", "return", "sizeof", "switch", "typedef", "while",
/*---------------------------------------------------------------
* saa c extensions
*---------------------------------------------------------------*/
"_Packed","_System","_Optlink", "_Far16", "_Cdecl", "_Pascal"
};
/*------------------------------------------------------------------
* c++ reserved words
*------------------------------------------------------------------*/
static char *CppReservedTokens[] =
{
"catch", "class", "delete", "friend", "inline", "new", "operator",
"private", "protected", "public", "template", "this", "throw", "try",
"virtual"
};
/*------------------------------------------------------------------
* generate an error message and exit
*------------------------------------------------------------------*/
void cPostError(
int exitCode,
char *format,
...
)
{
va_list vlist;
fprintf(stderr,"%s : ",PROGRAM_NAME);
va_start(vlist,format);
vfprintf(stderr,format,vlist);
va_end(vlist);
fprintf(stderr,"\n");
if (exitCode)
exit(exitCode);
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* print some help, assuming me is argv[0]
*------------------------------------------------------------------*/
static void Usage(void)
{
fprintf(stderr,"%s %s by %s %s\n",PROGRAM_NAME,PROGRAM_VERS,PROGRAM_AUTH,PROGRAM_ADDR);
fprintf(stderr,"\n");
fprintf(stderr,"usage:\n");
fprintf(stderr," %s <options> <filespec> <filespec> ...\n",PROGRAM_NAME);
fprintf(stderr,"is used to produce a listing of C language files in PostScript\n");
fprintf(stderr,"format. The PostScript output is written to stdout.\n\n");
fprintf(stderr,"where:\n");
fprintf(stderr," <filespec> is a filespec matching C language files.\n\n");
fprintf(stderr,"Valid options are:\n");
fprintf(stderr," -b[+|-] - enable/disable bracketing around braces\n");
fprintf(stderr," -cext1,ext2,... - treat files with extention ext1 and ext2 as C files\n");
fprintf(stderr," -d[+|-] - enable/disable duplex\n");
fprintf(stderr," -hext1,ext2,... - treat files with extention ext1 and ext2 as H files\n");
fprintf(stderr," -ifile1;file2;... - imbed files into output\n");
fprintf(stderr," -kk1,k2,... - treat k1, k2 as reserved (key=c++ adds c++ keywords)\n");
fprintf(stderr," -n# - separate line numbers from lines with # spaces\n");
fprintf(stderr," -n0 - do not generate line numbers\n");
fprintf(stderr," -ofile - output written to file (instead of stdout)\n");
fprintf(stderr," -p[+|-] - enable/disable page break at functions\n");
fprintf(stderr," -rfile1;file2,... - replace default PS procs with contents of files\n");
fprintf(stderr," -snt or -stn - sort files by name/type or type/name\n");
fprintf(stderr," -t# - expand tabs to # columns\n");
fprintf(stderr," -wf1,f2;f3,f4 - prepend files f1, f2 to output, append files f3, f4\n");
fprintf(stderr," -xx,y - coordinates for page adjustment\n");
fprintf(stderr," -ypath - path to use for temporary files\n");
fprintf(stderr," -? - display this help\n\n");
fprintf(stderr,"Default options are:\n");
fprintf(stderr," -b+ -d- -cc -hh -n2 -p+ -stn -t4 -x0,0\n");
fprintf(stderr,"Options may also be set in the environment variable %s.\n",PROGRAM_ENVV);
exit(1);
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------
* add default key words to reserved hash
*------------------------------------------------------------------*/
void InitializeReservedHash(
Info *info,
char *keyList
)
{
int i;
char *part;
/*---------------------------------------------------------------
* create hash table
*---------------------------------------------------------------*/
info->reservedHash = HashCreate(sizeof(char *),
30,
(HashFunc *)IdentHash,
(ListCompareFunc *)IdentCompare,
cPostNoMem);
if (!info->reservedHash)
cPostError(1,"error creating reserved word hash table");
for (i=0; i<sizeof(ReservedTokens)/sizeof(char *); i++)
if (!HashAdd(info->reservedHash,&(ReservedTokens[i])))
cPostError(1,"error adding reserved word '%s' to hash table",
ReservedTokens[i]);
/*---------------------------------------------------------------
* loop through the comma separated keys ...
*---------------------------------------------------------------*/
part = strtok(keyList,",");
while (part)
{
/*------------------------------------------------------------
* special c++ token
*------------------------------------------------------------*/
if (!Stricmp("c++",part))
{
for (i=0; i<sizeof(CppReservedTokens)/sizeof(char *); i++)
if (!HashAdd(info->reservedHash,&(CppReservedTokens[i])))
cPostError(1,"error adding reserved word '%s' to hash table",
CppReservedTokens[i]);
}
/*------------------------------------------------------------
* file name
*------------------------------------------------------------*/
else if (('@' == part[0]) && (1 != strlen(part)))
{
TokFileInfo tfi;
char *key;
part++;
tfi = TokFileOpen(part);
if (!tfi)
cPostError(0,"error opening file '%s' for reading",part);
else
{
while (NULL != (part = TokFileNext(tfi)))
{
key = malloc(1 + strlen(part));
if (!key)
cPostError(1,"out of memory!!");
strcpy(key,part);
if (!HashAdd(info->reservedHash,&key))
cPostError(0,"error adding reserved word '%s' to hash table; word ignored",
key);
}
}
}
/*------------------------------------------------------------
* plain old